package mb.view;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import mb.config.Para;                 // +

public class AppMenu extends JMenuBar  implements ActionListener {

  // {+
  private Para para = Para.getPara();

  private static String file;
  private static String exit;
  private static String edit;
  private static String selectBoxes;
  private static String deselectBoxes;
  private static String deleteSelectedBoxes;
  private static String view;
  private static String hideSelectedBoxes;
  private static String showSelectedBoxes;
  private static String help;
  private static String about;

  {
    file =	              para.getT("file");
    exit =	              para.getT("exit");
    edit =	              para.getT("edit");
    selectBoxes =	        para.getT("selectBoxes");
    deselectBoxes =	      para.getT("deselectBoxes");
    deleteSelectedBoxes = para.getT("deleteSelectedBoxes");
    view =                para.getT("view");
    hideSelectedBoxes =	  para.getT("hideSelectedBoxes");
    showSelectedBoxes =	  para.getT("showSelectedBoxes");
    help =	              para.getT("help");
    about =	              para.getT("about");
  }
  // +}

  private static final int EXIT			= 11;
  private static final int SELECT		= 21;
  private static final int DESELECT = 22;
  private static final int DELETE		= 23;
  private static final int HIDE			= 31;
  private static final int SHOW			= 32;
  private static final int ABOUT		= 41;

  // {&
  private JMenu		  menuFile		     = new JMenu(file);
  private JMenuItem menuFileExit	   = new JMenuItem(exit);
  private JMenu 	  menuEdit 		     = new JMenu(edit);
  private JMenuItem menuEditSelect 	 = new JMenuItem(selectBoxes);
  private JMenuItem menuEditDeselect = new JMenuItem(deselectBoxes);
  private JMenuItem menuEditDelete   = new JMenuItem(deleteSelectedBoxes);
  private JMenu 	  menuView		     = new JMenu(view);
  private JMenuItem menuViewHide 	   = new JMenuItem(hideSelectedBoxes);
  private JMenuItem menuViewShow 	   = new JMenuItem(showSelectedBoxes);
  private JMenu 	  menuHelp		     = new JMenu(help);
  private JMenuItem menuHelpAbout 	 = new JMenuItem(about);
  // &}

  private AppFrame appFrame;

  public AppMenu() {
  	super();
    menuFileExit.setActionCommand("Exit");
    menuFileExit.addActionListener(this);

    menuEditSelect.setActionCommand("Select");
    menuEditSelect.addActionListener(this);
    menuEditDeselect.setActionCommand("Deselect");
    menuEditDeselect.addActionListener(this);
    menuEditDelete.setActionCommand("Delete");
    menuEditDelete.addActionListener(this);

    menuViewHide.setActionCommand("Hide");
    menuViewHide.addActionListener(this);
    menuViewShow.setActionCommand("Show");
    menuViewShow.addActionListener(this);

    menuHelpAbout.setActionCommand("About");
    menuHelpAbout.addActionListener(this);

    menuFile.add(menuFileExit);
    menuEdit.add(menuEditSelect);
    menuEdit.add(menuEditDeselect);
    menuEdit.addSeparator();
    menuEdit.add(menuEditDelete);
    menuView.add(menuViewHide);
    menuView.add(menuViewShow);
    menuHelp.add(menuHelpAbout);

    this.add(menuFile);
    this.add(menuEdit);
    this.add(menuView);
    this.add(menuHelp);
	}

  public AppMenu(AppFrame aFrame) {
    this();
    this.setAppFrame(aFrame);
  }

  public AppFrame getAppFrame() {
  	return appFrame;
  }

  public void setAppFrame(AppFrame aFrame) {
  	appFrame = aFrame;
  }

  private void displayAbout() {
    AppAbout about = new AppAbout(appFrame);
    Dimension aboutSize = about.getPreferredSize();
    Dimension frameSize = appFrame.getSize();
    Point frameLocation = appFrame.getLocation();
    int x = ((frameSize.width - aboutSize.width) / 2) + frameLocation.x;
    int y = ((frameSize.height - aboutSize.height) / 2) + frameLocation.y;
    about.setLocation(x, y);
    about.setModal(true);
    about.setVisible(true);
  }

  private int getCommand(String aCommand) {
    int	action = -1;
    if      (aCommand.equals("Exit"))     action = EXIT;
    else if (aCommand.equals("Select"))   action = SELECT;
    else if (aCommand.equals("Deselect")) action = DESELECT;
    else if (aCommand.equals("Delete"))   action = DELETE;
    else if (aCommand.equals("Hide"))     action = HIDE;
    else if (aCommand.equals("Show"))     action = SHOW;
    else if (aCommand.equals("About"))    action = ABOUT;
    return action;
  }

  public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
    switch (this.getCommand(command)) {
    	case EXIT:      appFrame.exit();
                      break;
    	case SELECT:    appFrame.getDiagram().selectAllBoxes();
                      break;
    	case DESELECT:  appFrame.getDiagram().deSelectAllBoxes();
                      break;
      case DELETE:    appFrame.getDiagram().deleteSelectedBoxes();
                      break;
    	case HIDE:      appFrame.getDiagram().hideSelectedBoxes();
                      break;
    	case SHOW:      appFrame.getDiagram().showSelectedBoxes();
                      break;
      case ABOUT:     this.displayAbout();
                      break;
    }
  }

}
